package com.own.component.common.model;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.own.base.BaseResponseCode;
import com.own.constant.ConstantCommon;
import com.own.constant.ConstantDateFormatPattern;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.slf4j.MDC;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * BaseResponseModel
 *
 * @author chenxueli
 * @date 2020/8/18 10:01
 */
@Getter
@Setter
@NoArgsConstructor
public abstract class BaseResponseModel implements Serializable {

    @Schema(title = "响应状态码")
    private String code;

    @Schema(title = "响应消息")
    private String message;

    @Schema(title = "系统时间")
    @JsonFormat(pattern = ConstantDateFormatPattern.Pattern.DATETIME, timezone = "GMT+8")
    private LocalDateTime t;

    @Schema(title = "请求是否成功")
    private Boolean success;

    @Schema(title = "日志链路跟踪id")
    private String traceId;

    /**
     * 构造方法
     *
     * @param code    响应状态码
     * @param message 响应消息
     */
    public BaseResponseModel(final String code, final String message) {
        this.code = code;
        this.message = message;
        this.t = LocalDateTime.now();
        this.success = true;
        this.traceId = MDC.get(ConstantCommon.TRACE_ID);
    }

    /**
     * 构造方法
     *
     * @param responseCode 响应枚举
     */
    public BaseResponseModel(final BaseResponseCode responseCode) {
        responseCode(responseCode);
    }

    /**
     * 设置响应枚举信息
     *
     * @param responseCode 响应枚举
     */
    public final void responseCode(final BaseResponseCode responseCode) {
        this.code = responseCode.code();
        this.message = responseCode.desc();
        this.t = LocalDateTime.now();
        this.success = true;
        this.traceId = MDC.get(ConstantCommon.TRACE_ID);

    }

    /**
     * 设置响应枚举内容
     *
     * @param code    响应状态码
     * @param message 响应消息
     */
    public final void content(final String code, final String message) {
        this.code = code;
        this.message = message;
        this.t = LocalDateTime.now();
        this.success = true;
        this.traceId = MDC.get(ConstantCommon.TRACE_ID);
    }

}
